package com.tnovoselec.android.mojevijesti.parser;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.os.SystemClock;
import android.util.Log;

import com.tnovoselec.android.mojevijesti.dto.NewsItemDto;

public class NewsParser extends DefaultHandler {

	private static final String TAG_ITEM = "item";
	private static final String TAG_LINK = "link";

	private static final String TAG_CONTENT = "content";
	private static final String TAG_TITLE = "title";
	private static final String TAG_PUBDATE = "pubDate";

	private static final String TAG_DESCRIPTION = "description";
	private static final String TAG_IMAGES = "images";
	private static final String TAG_IMAGE = "image";

	private boolean inItem = false;
	private boolean inLink = false;

	private boolean inContent = false;
	private boolean inPubdate = false;
	private boolean inTitle = false;

	private boolean inDescription = false;
	private boolean inImages = false;
	private boolean inImage = false;

	private List<NewsItemDto> news = new ArrayList<NewsItemDto>();
	private NewsItemDto item;
	private long parsingStartTime;

	private StringBuilder buffer;

	@Override
	public void startDocument() throws SAXException {
		parsingStartTime = SystemClock.elapsedRealtime();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		buffer = new StringBuilder("");

		if (localName.equalsIgnoreCase(TAG_ITEM)) {
			inItem = true;
			item = new NewsItemDto();
		} else if (localName.equalsIgnoreCase(TAG_TITLE)) {
			inTitle = true;
		} else if (localName.equalsIgnoreCase(TAG_LINK)) {
			inLink = true;

		} else if (localName.equalsIgnoreCase(TAG_CONTENT)) {
			inContent = true;
		} else if (localName.equalsIgnoreCase(TAG_PUBDATE)) {
			inPubdate = true;
		} else if (localName.equalsIgnoreCase(TAG_DESCRIPTION)) {
			inDescription = true;
		} else if (localName.equalsIgnoreCase(TAG_IMAGES)) {
			inImages = true;

		} else if (localName.equalsIgnoreCase(TAG_IMAGE)) {
			inImage = true;

		}
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if (inItem) {
			if (inTitle) {
				item.setTitle(buffer.toString());
			} else if (inLink) {
				item.setLink(buffer.toString());
			} else if (inPubdate) {
				item.setPubDate(buffer.toString());
			} else if (inDescription) {
				item.setDescription(buffer.toString());
			} else if (inContent) {
				item.setContent(buffer.toString());
			} else if (inImages) {
				if (inImage) {
					item.getImages().add(buffer.toString());
				}
			}
		}
		if (localName.equalsIgnoreCase(TAG_ITEM)) {
			inItem = false;
			news.add(item);
		} else if (localName.equalsIgnoreCase(TAG_TITLE)) {
			inTitle = false;
		} else if (localName.equalsIgnoreCase(TAG_LINK)) {
			inLink = false;
		} else if (localName.equalsIgnoreCase(TAG_CONTENT)) {
			inContent = false;
		} else if (localName.equalsIgnoreCase(TAG_PUBDATE)) {
			inPubdate = false;
		} else if (localName.equalsIgnoreCase(TAG_DESCRIPTION)) {
			inDescription = false;
		} else if (localName.equalsIgnoreCase(TAG_IMAGES)) {
			inImages = false;
		} else if (localName.equalsIgnoreCase(TAG_IMAGE)) {
			inImage = false;
		}
	}

	@Override
	public void endDocument() throws SAXException {
		Log.i(getClass().getName(), "XML parsing took " + (SystemClock.elapsedRealtime() - parsingStartTime) / 1000f + " sec");
		Log.i(getClass().getName(), "Have " + news.size() + " news");
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		buffer.append(String.valueOf(ch, start, length));
	}

	public List<NewsItemDto> getNews() {
		return news;
	}

}
